<?php
namespace app\m\controller;

use app\common\controller\HomeBase;
use think\Db;
use app\common\model\User as UserModel;
use think\facade\Cache;
use think\facade\Config;
use think\facade\Session;
use think\facade\Url;

class User extends HomeBase
{
    protected function initialize()
    {
        parent::initialize();
        $this->checkLogin();

    }

    public function index()
    {
        $this->site_seo("home");

        $vip = model('vip')->where(['id' => $this->user_info['vip_id']])->find();
        $this->site_seo("home");
        return $this->fetch('index',['index_style' => 'user','vip' => $vip]);
    }

    public function login()
    {
        if ($this->request->isPost())
        {
            $phone = trim($this->request->param('phone'));
            $code = trim($this->request->param('code'));

            if (Cache::get("verify_code_" . md5($this->request->ip())) == $code)
            {
                if (!UserModel::checkUserIsExist("phone",$phone,'')) {
                    UserModel::createUser($phone,'',$phone, md5(rand(1000, 10000) . time()), 0, "phone",'');
                }
                UserModel::userLogin("phone",$phone,'');
                return $this->echoJson("",200,"登录成功");
            }else {
                return $this->echoJson("",-1,"验证码不匹配");
            }
        }

    }


    /**
     * 退出
     * @return mixed
     */
    public function logout()
    {
        Session::set('user_info', "");
        $this->success("退出成功");
    }
}
